package com.fy56.platform.logistics.restful.api.controller;

import com.fy56.platform.logistics.restful.api.controller.wx.OrderController;
import com.fy56.platform.logistics.restful.api.utils.ConstansUtils;
import com.fy56.platform.logistics.restful.api.utils.StringToConversionUtil;
import com.fy56.platform.logistics.restful.api.utils.redis.RedisBusinessUtils;
import com.fy56.platform.logistics.restful.api.utils.weChat.WeChatOfficialAccountUtil;
import com.fy56.platform.logistics.service.common.IRedisService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * [控制器]外部页面-引入
 */
@Controller
public class ExternalVewController {

	@Autowired
	private RedisBusinessUtils redisBusinessUtils;

	/*----------------------------------------------------------------------------------------------------*/

	private static Logger log = Logger.getLogger(OrderController.class);

	/*----------------------------------------------------------------------------------------------------*/

	/**
	 * [获取]微信签名
	 */
	@RequestMapping(value = "/getWsJsapiSign.htl", method = { RequestMethod.POST })
	public @ResponseBody Map<String, String> getJsapiSign(String url) throws Exception {
		log.info("===> 微信JS-SDK-验签_开始");
		Map<String, String> retMap = new LinkedHashMap<>();
		if(StringToConversionUtil.isNotNull(url)){
			url = new java.net.URI(url).getPath();
//			System.out.println("===> url:\t" + url);

			// [获取]微信访问密钥(accessToken)
			String accesstoken = redisBusinessUtils.getWxAppletAccessToken(ConstansUtils.SYS_CONSTANS.WECHAT_OFFICIAL_ACCOUNT_APPID, ConstansUtils.SYS_CONSTANS.WECHAT_OFFICIAL_ACCOUNT_SECRET);
//			System.out.println("===> accessToken:\t" + accesstoken);

			//[获取]微信JS-SDK-签名凭证(jsapi_ticket)
			String jsapiTicket = redisBusinessUtils.getWxJsApiTicket(accesstoken);
//			System.out.println("===> jsapi_ticket:\t" + jsapiTicket);

			// [获取]微信JS-SDK_微信签名
			Map<String, String> jsapiSign = WeChatOfficialAccountUtil.sign(jsapiTicket, url, ConstansUtils.SYS_CONSTANS.WECHAT_OFFICIAL_ACCOUNT_APPID);
			retMap.putAll(jsapiSign);
			retMap.put("textStatus", "success");
		}else{
			retMap.put("textStatus", "error");
		}
		log.info("===> 微信验签 retMap:"+retMap.toString());
//		System.out.println("===> 外网ip:"+getV4IP());
		return retMap;
	}

	/*----------------------------------------------------------------------------------------------------*/

//	@RequestMapping(value = "/", method = { RequestMethod.GET })
//	public String index(Map<String, Object> dataMap) {
//		return "redirect:mainView.html";
//	}
	
	/**
	 * 管家婆飞扬合作页面
	 */
	@RequestMapping(value = "/housekeeper_code.htl", method = { RequestMethod.GET })
	public String housekeeper_code(Map<String, Object> dataMap, HttpServletRequest request) {
		return "externalVew/housekeeper_code/login";
	}

	/*----------------------------------------------------------------------------------------------------*/

	@Autowired
	private IRedisService iRedisService;

	/**
	 * test页面
	 */
	@RequestMapping(value = "/test.htl", method = { RequestMethod.GET })
	public void test(Map<String, Object> dataMap, HttpServletRequest request) {

		iRedisService.set("aa022", "1111aaaa", 1000);

	}
}
